package utiles.net
{
	import org.osflash.thunderbolt.Logger;

	public class NetworkManager
	{
		
		public function NetworkManager()
		{
			initServer();
		}
		
		private static var _instance:NetworkManager = null;
		public static function get instance():NetworkManager
		{
			if (_instance == null)
				_instance = new NetworkManager();
			return _instance;
		}
		
		private var _serv:VirtualServer;
		private function initServer():void
		{
			_serv = new VirtualServer();
			_serv.addEventListener("throw_msg", onRecvServMsg);
		}
		
		protected function onRecvServMsg(event:VirtualServerEvent):void
		{
			Logger.warn("socket 收到" + QProtocol.getName(event.msg.type));
			
			if (_dataHandler != null)
				_dataHandler(event.msg);
		}
		
		public function sendMsg(protocol:QProtocol):void
		{
			Logger.warn("socket 发送" + protocol.type);
			_serv.recv(protocol);
		}
		
		private var _dataHandler:Function = null;
		public function setDataHandler(handler:Function):void
		{
			_dataHandler = handler;
		}
	}
}